package cn.iocoder.yudao.module.member.eid.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.module.api.member.agency.message.AgencyAuditPassMessage;
import cn.iocoder.yudao.module.api.member.agency.vo.AgencyApplyVO;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.api.shop.info.message.ShopAuditPassMessage;
import cn.iocoder.yudao.module.member.eid.service.MemberEidService;
import cn.iocoder.yudao.module.member.eid.vo.AppEidSaveReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 实名信息 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class MemberEidConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ShopAuditPassMessage message) {
        log.info("[member/eid][onMessage][商家审核通过事件({})]", message);
        ShopInfoDTO shopInfo = message.getShopInfo();
        Assertions.notNull(shopInfo, "商家审核通过事件携带的申请信息为空");
        saveEidInfo(shopInfo.getUserId(), shopInfo.getContactName(), shopInfo.getOwnerEidNo(), shopInfo.getOwnerEidFrontUrl(), shopInfo.getOwnerEidBackUrl());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(AgencyAuditPassMessage message) {
        log.info("[member/eid][onMessage][招商人员审核通过事件({})]", message);
        AgencyApplyVO applyInfo = message.getApplyInfo();
        Assertions.notNull(applyInfo, "招商人员审核通过事件携带的申请信息为空");
        saveEidInfo(applyInfo.getUserId(), applyInfo.getContactName(), applyInfo.getOwnerEidNo(), applyInfo.getOwnerEidFrontUrl(), applyInfo.getOwnerEidBackUrl());
    }

    private void saveEidInfo(Long userId, String realName, String idcardNo, String idcardFrontUrl, String idcardBackUrl) {
        AppEidSaveReqVO reqVO = new AppEidSaveReqVO();
        reqVO.setRealName(realName);
        reqVO.setIdcardNo(idcardNo);
        reqVO.setIdcardFrontUrl(idcardFrontUrl);
        reqVO.setIdcardBackUrl(idcardBackUrl);
        SpringUtil.getBean(MemberEidService.class).saveIdcard(userId, reqVO);
    }

}
